package com.woniu.microcommunityserver.facility.mapper;

import com.woniu.microcommunityserver.facility.entity.CommunalFacilityInfoEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface CommunalFacilityInfoMapper {
    /**
     * 查询所有设施
     * @return
     */
    @Select("select id,facility_name,facility_picture_storage_address,facility_location,facility_opening_time," +
            "village_name,facility_state from communal_facility_info")
    @ResultMap("communalFacilityInfoMap")
    List<CommunalFacilityInfoEntity> findAll();

    /**
     * 根据条件查询设施
     * @param map
     * @return
     */
    @Select({"<script> " +
            "select id,facility_name,facility_picture_storage_address,facility_location," +
            "facility_opening_time,village_name,facility_state from communal_facility_info" +
            "<where>" +
            "<if test='facilityName!= null'>" +
            "and facility_name like concat('%', #{facilityName}, '%')"+
            "</if>"+
            "<if test='villageName!= null'>" +
            "and village_name = #{villageName}"+
            "</if>"+
            "<if test='facilityState!= null'>" +
            "and facility_state = #{facilityState}"+
            "</if>"+
            "</where>"+
            "</script>"})
    @ResultMap("communalFacilityInfoMap")
    List<CommunalFacilityInfoEntity> findAny(Map<String,Object> map);

    /**
     * 新增设施
     * @param communalFacilityInfoEntity
     * @return
     */
    @Insert("insert into communal_facility_info(facility_name,facility_picture_storage_address," +
            "facility_location,facility_opening_time,village_name,facility_state) values(#{c.facilityName}," +
            "#{c.facilityPictureStorageAddress},#{c.facilityLocation},#{c.facilityOpeningTime}," +
            "#{c.villageName},#{c.facilityState})")
    boolean insertFacility(@Param("c") CommunalFacilityInfoEntity communalFacilityInfoEntity);

    /**
     * 根据id删除设施
     * @param id
     * @return
     */
    @Delete("delete from communal_facility_info where id=#{id}")
    boolean deleteFacility(Integer id);

    /**
     * 编辑设施
     * @param communalFacilityInfoEntity
     * @return
     */
    @Update({"<script>",
            "update communal_facility_info",
            "<set>",
            "<if test='facilityName!= null'>",
            "facility_name = #{facilityName} ,",
            "</if>",
            "<if test='facilityPictureStorageAddress != null'>",
            "facility_picture_storage_address = #{facilityPictureStorageAddress} ,",
            "</if>",
            "<if test='facilityLocation != null'>",
            "facility_location = #{facilityLocation} ,",
            "</if>",
            "<if test='facilityOpeningTime != null'>",
            "facility_opening_time = #{facilityOpeningTime} ,",
            "</if>",
            "<if test='villageName != null'>",
            "village_name = #{villageName} ,",
            "</if>",
            "<if test='facilityState != null'>",
            "facility_state = #{facilityState} ,",
            "</if>",
            "</set>",
            "where id = #{id}",
            "</script>"})
    boolean updateFacility(CommunalFacilityInfoEntity communalFacilityInfoEntity);
}
